package everyday.nowcoder;

/**
 * @Author: wenjingyuan
 * @Date: 2022/10/25/16:34
 * @Description:NC33合并两个有序的链表(完全掌握)
 */
public class NumNC33 {
    public ListNode Merge(ListNode list1,ListNode list2) {
        //新链表的虚拟头结点
        ListNode dummyHead=new ListNode(-1);
        ListNode res=dummyHead;
        while(list1!=null && list2!=null){
          if(list1.val>=list2.val){
              dummyHead.next=list2;
              list2=list2.next;
              //复习重点（谨记）
              dummyHead=dummyHead.next;
          }else{
              dummyHead.next=list1;
              list1=list1.next;
              dummyHead=dummyHead.next;
          }
        }
        if(list1==null && list2!=null){
            dummyHead.next=list2;
        }
        if(list1!=null && list2==null){
            dummyHead.next=list1;
        }
        if(list1==null && list2==null){
            return res.next;
        }
        return res.next;
    }
}
